<style lang="less">
    @import '../../styles/common.less';
</style>

<template>
<div>
        <i-Form :model="model" :label-width="100">
      
                <Card>  
        <p slot="title">
            <Icon type="navicon-round"></Icon>
            角色<span>{{pagename}}</span>
        </p>

            <Form-item label="角色名">
                <i-Input type="text" v-model="model.typename" placeholder="角色名">
                </i-Input>
            </Form-item>            
             <Form-item>
                 <Checkbox :indeterminate="indeterminate" :value="checkAll"
            @click.prevent.native="handleCheckAll">全选</Checkbox>
            </Form-item>            
             <Form-item>
                 <Checkbox-group v-model="values" @on-change="checkAllGroupChange">
                    <Checkbox label="system">系统设置</Checkbox>
                    <Checkbox label="user">用户管理</Checkbox>
                    <Checkbox label="usertype">角色管理</Checkbox>
                    <Checkbox label="log">日志管理</Checkbox>
                    <Checkbox label="article">文章管理</Checkbox>
                    <Checkbox label="category">分类管理</Checkbox>
                    <Checkbox label="jxinxi">简内容管理</Checkbox>
                    <Checkbox label="nav">导航管理</Checkbox>
                    <Checkbox label="links">友情链接管理</Checkbox>
                    <Checkbox label="ad">广告管理</Checkbox>
                </Checkbox-group>
             </Form-item>
            <Form-item>
                <i-button type="primary" @click="save">提交</i-button>
            </Form-item>
    </Card>
        </i-Form>
    </div>
    
</template>

<script>
import api from '../../libs/api';
export default {
    name: 'usertype_indexadd',
    data () {
        return {            
            pagename:"添加",
            model:{},
            indeterminate: true,
            checkAll: false,
            values:[]
        };
    },
    methods: {     
            handleCheckAll () {
                if (this.indeterminate) {
                    this.checkAll = false;
                } else {
                    this.checkAll = !this.checkAll;
                }
                this.indeterminate = false;

                if (this.checkAll) {
                    this.values = ['system', 'user', 'usertype','log','article', 'category', 'jxinxi','nav', 'links','ad'];
                } else {
                    this.values = [];
                }
            },
        checkAllGroupChange(data){
                if (data.length === 10) {
                    this.indeterminate = false;
                    this.checkAll = true;
                } else if (data.length > 0) {
                    this.indeterminate = true;
                    this.checkAll = false;
                } else {
                    this.indeterminate = false;
                    this.checkAll = false;
                }
        },
        save(){            
            var vm=this;     
            this.model.purviews=this.values.toString();
            api.post("/admintype/save",this.model).then(function(res){
                if(vm.model.ID>0){
                    vm.$Message.success("修改成功！");
                    }
                else                    
                    vm.$Message.success("添加成功！");
                    vm.$router.push({
                        name: 'usertype_index',
                        query: {}
                    });
            })
        },
        init () {
            var vm=this;
            var id=this.$route.query.ID;
            if(id>0){
                this.pagename="编辑"; 
                api.get("/admintype/get/"+id).then(function(res){
                    vm.model=res.data.msg;   
                    if(vm.model.purviews=='allowall'){
                        vm.values=['system', 'user', 'usertype','log','article', 'category', 'jxinxi','nav', 'links','ad'];
                        console.log("超管");
                    }
                    else if(vm.model.purviews==undefined||vm.model.purviews==''){
                        vm.indeterminate=false;
                        vm.values=[];
                    }
                    else{
                        vm.values=vm.model.purviews.split(',');
                        if (vm.values.length === 10) {
                            vm.indeterminate = false;
                            vm.checkAll = true;
                        }
                    }

                })
            }
            else{
                this.pagename="添加";             
                this.$nextTick(() => {

                })
            }
        }
    },
    mounted () {
        this.init();
    },
    watch: {
        '$route' () {
            this.init();
        }
    }

};
</script>
